/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2emu.gameserver.model.actor.instance;

import com.l2emu.Config;
import com.l2emu.gameserver.datatables.GmListTable;
import com.l2emu.gameserver.network.serverpackets.NpcHtmlMessage;
import com.l2emu.gameserver.network.serverpackets.PlaySound;
import com.l2emu.gameserver.templates.chars.L2NpcTemplate;
import com.l2emu.gameserver.util.Util;

/**
 * Instance to manage jailed players :)
 *
 * @author Rayan RPG for L2Emu Project !
 * @since 471
 * @project L2Emu Project
 */
public class L2JailManagerInstance extends L2NpcInstance
{
	/**
	 *
	 * @param objectId
	 * @param template
	 */
	public L2JailManagerInstance(int objectId, L2NpcTemplate template)
	{
		super(objectId, template);
		// TODO Auto-generated constructor stub
	}

	@Override
	public String getHtmlPath(int npcId, int val)
	{
		String pom = "";

		if (val == 0)
			pom = "" + npcId;
		else
			pom = npcId + "-" + val;

		return "data/html/mods/jailManager/" + pom + ".htm";
	}

	@Override
	public void onBypassFeedback(L2PcInstance player, String command)
	{
		if (command.equalsIgnoreCase("start_mission") && Config.ALLOW_JAILMANAGER)
		{
			//informs player what he have to do to leave jail.
			NpcHtmlMessage html = new NpcHtmlMessage(getObjectId());
			html.setFile("data/html/mods/jailManager/mission.htm");
			html.replace("%objectId%", String.valueOf(getObjectId()));
			html.replace("%playername%", player.getName());
			html.replace("%amount%", String.valueOf(Config.REQUIRED_JAIL_POINTS));
			html.replace("%npcname%", String.valueOf(getName()));
			player.sendPacket(html);

			//don't allow a dead or fake death player to perform actions
			if (player.isDead() || player.isFakeDeath())
			{
				player.sendMessage("Cant leave jail while dead or fake death.");
				return;
			}
		}
		else if (command.equalsIgnoreCase("check_points") && player.isInJailMission())
		{
			//informs player currently amount of points
			NpcHtmlMessage html2 = new NpcHtmlMessage(getObjectId());
			html2.setFile("data/html/mods/jailManager/points.htm");
			html2.replace("%objectId%", String.valueOf(getObjectId()));
			html2.replace("%points%", String.valueOf(player.getJailPoints()));
			html2.replace("%rest%", String.valueOf(Config.REQUIRED_JAIL_POINTS - player.getJailPoints()));
			html2.replace("%npcname%", String.valueOf(getName()));
			player.sendPacket(html2);

			//don't allow a dead or fake death player to perform actions
			if (player.isDead() || player.isFakeDeath())
			{
				player.sendMessage("Cant check points while dead or fake death.");
				return;
			}
		}
		else if (command.equalsIgnoreCase("get_mission"))
		{
			if (player.isInJailMission())
			{
				player.sendPacket(new NpcHtmlMessage(player.getObjectId(),"data/html/mods/jailManager/mission_already.htm"));
				return;
			}

			//sets jail mission state
			player.setIsInJailMission(true);

			//plays a sound for mission start
			PlaySound ps = new PlaySound(0, "ItemSound2.race_start", 0, player.getObjectId(), player.getX(), player.getY(), player.getZ());
			player.sendPacket(ps);

			//sends a informative window
			player.sendPacket(new NpcHtmlMessage(player.getObjectId(),"data/html/mods/jailManager/started.htm"));

			//don't allow a dead or fake death player to perform actions
			if (player.isDead() || player.isFakeDeath())
			{
				player.sendMessage("Cant get mission while dead or fake death.");
				return;
			}
		}
		else if (command.equalsIgnoreCase("finish_mission"))
		{
			//informs player that he has not even started a mission if not started mission
			if (!player.isInJailMission())
			{
				player.sendPacket(new NpcHtmlMessage(player.getObjectId(),"data/html/mods/jailManager/notstarted.htm"));
				return;
			}

			//checks if player has enough jail points
			if (player.getJailPoints() < Config.REQUIRED_JAIL_POINTS)
			{
				if (Config.DEVELOPER)
				{
					_log.info("JailManager: Required points: " + Config.REQUIRED_JAIL_POINTS);
					_log.info("JailManager: Player points: " + player.getJailPoints());
				}
				//informs player that mission is not complete
				player.sendPacket(new NpcHtmlMessage(player.getObjectId(),"data/html/mods/jailManager/notcompleted.htm"));

				//notify gms if enabled
				if (Config.NOTIY_ADMINS_OF_ILLEGAL_ACTION)
					GmListTable.broadcastMessageToGMs("Player " + player.getName() + " is trying to leave Jail Without Required Jail Points!");
				return;
			}

			//dont allows a dead or fake death player to perform actions
			if (player.isDead() || player.isFakeDeath())
			{
				player.sendMessage("Cant finish mission while dead or fake death.");
				return;
			}

			//plays a mission end sound
			PlaySound ps = new PlaySound(0, "ItemSound.quest_finish", 0, player.getObjectId(), player.getX(), player.getY(), player.getZ());
			player.sendPacket(ps);

			//informs player that mission is complete
			player.sendPacket(new NpcHtmlMessage(player.getObjectId(), "data/html/mods/jailManager/completed.htm"));

			//sleeps thread a little just to player read stuff and hear sound :D
			try
			{
				Thread.sleep(Util.convertSecondsToMiliseconds(5));
			}
			catch (Throwable t)
			{
				_log.warning("Error, L2JailManagerInstance, reason: " + t.getMessage());
			}

			//removes player from jail
			player.setPunishLevel(L2PcInstance.PunishLevel.NONE, 0);
			
			//resets player jail points to 0
			player.resetJailPoints();
			player.sendMessage("Your jail points have been reset.");

			//resets jail mission state
			player.setIsInJailMission(false);
		}
		super.onBypassFeedback(player, command);
	}
}